package com.cs.beneficios.web.action;

import com.cs.paoyer.administration.business.model.UserVO;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;
import com.cs.beneficios.common.ConnectionFactory;

public class ModProgramaAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String valor1 = null, valor2 = null, valor3 = null, valor4 = null, valor5 = null;
		String valor6 = null, valor7 = null, valor8 = null, valor9 = null, valor10 = null;
		boolean existe = false;
		String strSql = "";
		// ConnectionFactory cf = new
		// ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
		// database db = new database();
		String strprog = request.getParameter("Programa");
		HttpSession session = request.getSession();
		Connection conn = null;
		try {
			conn = cf.getConnection();
		} catch (SQLException e) {
		}
		try {
			valor1 = request.getParameter("programa");
		} catch (Exception e) {
			valor1 = "";
		}
		try {
			valor2 = request.getParameter("objetivo");
		} catch (Exception e) {
			valor2 = "";
		}
		try {
			valor3 = request.getParameter("descripcion");
		} catch (Exception e) {
			valor3 = "";
		}
		try {
			valor4 = request.getParameter("area");
		} catch (Exception e) {
			valor4 = "";
		}
		try {
			valor5 = request.getParameter("admincodigo");
		} catch (Exception e) {
			valor5 = "";
		}
		try {
			valor10 = request.getParameter("codadmin");
		} catch (Exception e) {
			valor10 = "";

		}
		try {
			valor6 = request.getParameter("poblacion");
		} catch (Exception e) {
			valor6 = "";
		}
		try {
			valor7 = request.getParameter("desde");
		} catch (Exception e) {
			valor7 = "";
		}
		try {
			valor8 = request.getParameter("hasta");
		} catch (Exception e) {
			valor8 = "";
		}
		try {
			valor9 = request.getParameter("vigencia");
			if (valor9.equals("I")) {
				session.setAttribute("Programa", strprog);

				return mapping.findForward("success");
			}
		} catch (Exception e) {
			valor9 = "";
		}
		// Proceso para la auditoria
		UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
		String terminalAddress = request.getRemoteAddr();
		String strTerminal = ("WEB=" + terminalAddress);
		Calendar c = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		// Fin del proceso

		if (valor5.equalsIgnoreCase("")) {

			strSql = " update tcs_ben_programa set area_codigo = '"
					+ valor4
					+ "', usu_codigo = '"
					+ valor10
					+ "', "
					+ " nombre = UPPER('"
					+ valor1
					+ "'), descripcion = UPPER('"
					+ valor3
					+ "'), objetivo = UPPER('"
					+ valor2
					+ "'), "
					+ " poblacion_objetivo = UPPER('"
					+ valor6
					+ "'), desde_vigencia = TO_DATE('"
					+ valor7
					+ "','dd/mm/yyyy'), "
					+ " hasta_vigencia = TO_DATE('"
					+ valor8
					+ "','dd/mm/yyyy'), vigencia = '"
					+ valor9
					+ "' ,"
					+ " USUARIO_MODIFICA = ?, FECHA_MODIFICA = to_date(?,'dd/mm/yyyy'), "
					+ " TERMINAL_MODIFICA = ? " + " WHERE codigo = ? ";
		} else {
			strSql = " update tcs_ben_programa set area_codigo = '"
					+ valor4
					+ "', usu_codigo = '"
					+ valor5
					+ "', "
					+ " nombre = UPPER('"
					+ valor1
					+ "'), descripcion = UPPER('"
					+ valor3
					+ "'), objetivo = UPPER('"
					+ valor2
					+ "'), "
					+ " poblacion_objetivo = UPPER('"
					+ valor6
					+ "'), desde_vigencia = to_date('"
					+ valor7
					+ "','dd/mm/yyyy'), "
					+ " hasta_vigencia = TO_DATE('"
					+ valor8
					+ "','dd/mm/yyyy'), vigencia = '"
					+ valor9
					+ "', "
					+ " USUARIO_MODIFICA = ?, FECHA_MODIFICA = to_date(?,'dd/mm/yyyy'), "
					+ " TERMINAL_MODIFICA = ? " + " WHERE codigo = ? ";
		}
		String strPar[] = { user.getName(), dateFormat.format(c.getTime()),
				strTerminal, strprog };

		try {
			int modificar = cf.execSql(strSql, strPar, conn);
			if (modificar > 0) {
				session.setAttribute("mensajes",
						"El programa fue modificado correctamente!!!");
				session.setAttribute("primeraVez", null);
			}
			cf.closeConnection(conn);
		} catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSUB - cwsub ********/");
			session.setAttribute("mensajes",
					"El programa no se pudo modificar.");
		}

		return mapping.findForward("success2");
	}
}